package com.nian.user.center.service.impl;

import com.nian.common.model.SysMessage;
import com.nian.user.center.config.RoleType;
import com.nian.user.center.service.IAppUserService;
import com.nian.user.model.AppUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * 用途：
 * 开发：zhangmj
 * 日期：2020/4/5 0005 20:54
 */
@Service
public class UserServiceImpl implements UserDetailsService {

    @Autowired
    private IAppUserService iAppUserService;

    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        AppUser appUser = iAppUserService.getByUsername(userName);
        if(appUser != null){
            return new User(appUser.getUsername(), appUser.getPassword(), AuthorityUtils.commaSeparatedStringToAuthorityList(RoleType.ROLE_CLIENT));
        }
        throw new UsernameNotFoundException(SysMessage.USER_NAME_NOT_EXIST);
    }
}
